package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/getList")
    public Result getList(@RequestBody Map<String,Object> userVO){
        Page<User> userPage = userService.findAll(userVO);
        PageResult<User> pageResult=new PageResult<>();
        pageResult.setTotal(userPage.getTotal());
        pageResult.setRecords(userPage.getResult());
        pageResult.setSize((long) userPage.getPageSize());
        pageResult.setCurrent((long) userPage.getPageNum());
        pageResult.setOptimizeCountSql(true);
        pageResult.setSearchCount(true);
        return new Result("success",pageResult);
    }


    @PostMapping("/resetUserPassword")
    public Result resetUserPassword(@RequestBody Map<String,Object> userVO){
        userService.resetPassword(userVO);
        return new Result("success","操作成功");
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody Map<String,Object> userVO){
        userService.edit(userVO);
        return new Result("success","操作成功");
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Map<String,Object> userVO){
        userService.delete(userVO);
        return new Result("success","操作成功");
    }
}
